<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="药品名称" prop="drugname">
        <el-input
          v-model="queryParams.drugname"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售编号" prop="sellnum">
        <el-input
          v-model="queryParams.sellnum"
          placeholder="请输入销售编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店名称" prop="sname">
        <el-input
          v-model="queryParams.sname"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sell:sell:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sell:sell:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sell:sell:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sell:sell:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sellList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column label="销售编号" width="280" align="center" prop="sellnum" />
      <el-table-column label="药品名称" align="center" prop="drugname" />
      <el-table-column label="销售时间" align="center" prop="selltime" />
      <el-table-column label="销售数量" align="center" prop="sellquantity" />
      <el-table-column label="销售金额" align="center" prop="amount" />
      <el-table-column label="门店名称" align="center" prop="sid" />
      <el-table-column label="操作人" align="center" prop="opename" />
      <el-table-column label="创建时间" align="center" prop="createtime" />
      <el-table-column label="修改时间" align="center" prop="modifytime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sell:sell:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sell:sell:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="药品类别" prop="drugid">
          <el-select v-model="form.cid" placeholder="请选择药品类别" @change="getCategoryById">
            <el-option v-for="(item, index) in optionCategory" :key="index"
                       :label="item.cname" :value="item.cid" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="药品名称" prop="drugid">
          <el-select v-model="form.drugid" placeholder="请输入药品名称" @change="getDrugById">
            <el-option v-for="(item, index) in optionDrugs" :key="index"
                       :label="item.drugname" :value="item.drugid" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存名称" prop="storename">
          <el-input v-model="form.storename" readonly :value="form.storename" />
        </el-form-item>
        <el-form-item label="销售数量" prop="sellquantity">
          <el-input v-model="form.sellquantity" placeholder="请输入销售数量" />
        </el-form-item>
        <el-form-item label="门店名称" prop="sid">
          <el-input v-model="form.sid" placeholder="请输入门店名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSell, getSell, delSell, addSell, updateSell } from "@/api/sell/sell";
import { getAllCategory } from "@/api/category/category";
import { getByCategoryIdAllDrug, getDrugByIdStore } from "@/api/drug/drug";
export default {
  name: "Sell",
  data() {
    return {
      optionCategory:[],
      optionDrugs:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售管理表格数据
      sellList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drugname: null,
        storname: null,
        sellnum: null,
        sname: null
      },
      // 表单参数
      form: {storname: ''},
      // 表单校验
      rules: {
        drugid: [
          { required: true, message: "药品名称不能为空", trigger: "blur" }
        ],
        storid: [
          { required: true, message: "库存名称不能为空", trigger: "blur" }
        ],
        sellnum: [
          { required: true, message: "销售编号不能为空", trigger: "blur" }
        ],
        selltime: [
          { required: true, message: "销售时间不能为空", trigger: "blur" }
        ],
        sellquantity: [
          { required: true, message: "销售数量不能为空", trigger: "blur" }
        ],
        opename: [
          { required: true, message: "操作人不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "金额不能为空", trigger: "blur" }
        ],
        sid: [
          { required: true, message: "门店名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getAllCategories();
  },
  methods: {
    getDrugById(){
      getDrugByIdStore(this.form.drugid).then(res=>{
        this.form.storename=res.data.storename;
      })
    },
    getCategoryById(){
      getByCategoryIdAllDrug(this.form.cid).then(res=>{
      this.optionDrugs=res.data;
    })
    },
    getAllCategories(){
      getAllCategory().then(res=>{
        this.optionCategory=res.data;
      })
    },
    /** 查询销售管理列表 */
    getList() {
      this.loading = true;
      listSell(this.queryParams).then(response => {
        this.sellList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sellid: null,
        drugid: null,
        storid: null,
        sellnum: null,
        selltime: null,
        sellquantity: null,
        opename: null,
        amount: null,
        createtime: null,
        modifytime: null,
        deleted: null,
        sid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sellid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sellid = row.sellid || this.ids
      getSell(sellid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销售管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sellid != null) {
            updateSell(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSell(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sellids = row.sellid || this.ids;
      this.$modal.confirm('是否确认删除销售管理编号为"' + sellids + '"的数据项？').then(function() {
        return delSell(sellids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sell/sell/export', {
        ...this.queryParams
      }, `sell_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
